'''
Author: error: git config user.name && git config user.email & please set dead value or install git
Date: 2022-10-25 22:33:49
LastEditors: error: git config user.name && git config user.email & please set dead value or install git
LastEditTime: 2022-10-29 14:39:10
FilePath: \新建文件夹d:\多人猜数游戏\多人猜数游戏.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
#coding:utf-8
import time
import random
from tkinter import *
masterWindow = Tk()
masterWindow.title('猜数游戏')
masterWindow.geometry('400x550')
timesEvery=[[1,1000],[2,1000],[3,1000],[4,1000],[5,1000],[6,1000],[7,1000],[8,1000],[9,1000],[10,1000]]
X=[[1,1000],[2,1000],[3,1000],[4,1000],[5,1000],[6,1000],[7,1000],[8,1000],[9,1000],[10,1000]]
Label1 = Label (masterWindow,text='猜猜看',width=6,font= ('','18',''))
Label1.grid (row=1,column=0,rowspan=1,columnspan=1,pady=20,sticky="e")
guessValue = StringVar()
guessInput = Entry ( masterWindow,textvariable = guessValue,font= ('','18',''))
guessValue.set("")
guessInput.grid( row=1,column=6,rowspan=1,pady=20,columnspan=1)
Label2 = Label ( masterWindow,text='你猜的结果',width=10,font= ('','18',''))
Label2.grid ( row=3,column=0,rowspan=10,columnspan=1,pady=20,sticky="e")
outputLabel= Label ( masterWindow,text='等待游戏开始',width=20,font=('','18',''),bg="white")
outputLabel.grid(row=3, column=6,rowspan=10,columnspan=1,pady=20,sticky="w")
Label4 = Label(masterWindow, text='试猜次数',width=10,font= ('','18',''))
Label4.grid ( row=14,column=0,rowspan=10,columnspan=1,pady=20,sticky="e")
outputLabel_1= Label(masterWindow, text='',width=20,font= ('','18','') ,bg="white")
outputLabel_1.grid(row=14, column=6,rowspan=10,columnspan=1,pady=20,sticky="w")
okButton = Button ( masterWindow,text='确定',command = (lambda x=ALL: guessProcess()),width=8,height=1,font=('','20',''))
okButton.grid ( row=24,column=2,rowspan=5,columnspan=5,pady=20)
Label0 = Label ( masterWindow, text='请输入人数',width=10,font= ('','18',''))
Label0.grid ( row=30,column=0,rowspan=10,columnspan=1,pady=20,sticky="e")
numberPerson = StringVar ()
numberInput = Entry ( masterWindow,textvariable = numberPerson,font= ('','18',''))
numberPerson.set ("1")
numberInput.grid ( row=30,column=6,rowspan=5,pady=20,columnspan=1)
Label3= Label ( masterWindow,text='游戏结果',width=10,font= ('','18',''))
Label3.grid ( row=40,column=0,rowspan=10,columnspan=1,pady=20,sticky="e")
finalOutput= Label ( masterWindow,text='等待游戏开始',width=20,font= ('','18','') ,bg="white")
finalOutput.grid ( row=40,column=6,rowspan=10,columnspan=1,pady=20,sticky="w")
startButton = Button ( masterWindow,text='游戏开始',command = ( lambda x=ALL:guessStart()),width=8,height=1,font=('','20',''))
startButton.grid ( row=50,column=0,rowspan=5,columnspan=5,pady=20)
exitButton = Button ( masterWindow,text='退出',command = ( lambda x=ALL:exit()),width=8,height=1,font=('','20',''))
exitButton.grid ( row=50,column=2,rowspan=5,columnspan=5,pady=20)

def guessStart():
 global numGame,oneGame,Answer,startFlag,timesEvery,X,curPerson,timesCurrent;
 numGame = int ( numberPerson.get())
 startFlag = 1;
 curPerson = 0;
 Answer = random.randint (1,100);
 oneGame = 1;
 timesCurrent = 0;
 for k in range ( 0,10) :
   timesEvery[k][0] = k+1;
   timesEvery[k][1] = 1000;
   X[k][0]=k+1;
   X[k][1]=1000;
 finalOutput['text']='第一位'
 outputLabel['text']="请输入数字,按确定";  

def guessProcess ():
 global oneGame,Answer,startFlag,timesEvery,X,curPerson,timesCurrent;
 if ( startFlag==0 ):
    return
 if ( curPerson >= numGame or curPerson > 9 ):
    X = sorted ( timesEvery,cmp=None,key=lambda key:key[1],reverse=False)
    finalOutput['text'] = str ( X[0][0])
    for j in range ( 1, curPerson ):
      finalOutput['text'] = finalOutput['text'] + ":" + str (X[j][0])
    return
 elif ( oneGame==0 ) :
   outputLabel['text'] = '下一位继续'
   timesEvery[curPerson][1] = timesCurrent
   curPerson=curPerson+1
   Answer = random.randint ( 1,100 )
   timesCurrent = 0  
   oneGame = 1
   outputLabel_1['text'] = str ( timesCurrent)
   finalOutput['text']=""
   for j in range ( 0,curPerson ) :
     finalOutput['text'] = finalOutput['text'] + str ( timesEvery[j][0]) +":" +str ( timesEvery[j][1]) + "-"
 else:
   oneguessProcess ()

def oneguessProcess():
    global timesCurrent,oneGame;
    if ( oneGame == 1):
        result = int ( guessValue.get ())
        if result > Answer:
           outputLabel['text']='大了,继续努力...'
        elif result < Answer:
           outputLabel['text']='小了,继续努力...'
        else:
           outputLabel['text']='恭喜你,猜对了!'
           oneGame=0
        timesCurrent = timesCurrent + 1
    outputLabel_1['text'] = str (timesCurrent)


 
masterWindow.mainloop()